﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RegexLib.Web.Models.Data;

namespace RegexLib.Web.Models.Web
{
	/// <summary>
	/// 提供关键字相关操作。
	/// </summary>
	public static class RxKeywords
	{
		/// <summary>
		/// 按照文字获取关键字。
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static KeywordInfo GetKeywordByText(string text)
		{
			if (text == null)
				return null;

			using (RxDataContext db = new RxDataContext())
			{
				return db.KeywordInfos.FirstOrDefault(k => k.LoweredText == text.ToLower());
			}
		}

		/// <summary>
		/// 获取一个关键字下的正则表达式。
		/// </summary>
		/// <param name="keyword"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static RegexInfo[] GetRegex(this KeywordInfo keyword, int pageIndex, int pageSize, out int totalPages)
		{
			using (RxDataContext db = new RxDataContext())
			{
				var q = from r in db.RegexInfos
						join rk in db.RegexKeywords on r.RegexId equals rk.RegexId
						where rk.KeywordId == keyword.KeywordId
						group new { r, rk } by new { rk.KeywordId, r } into g
						orderby g.Count() descending
						select g.Key.r;

				int count = q.Count();
				totalPages = (count + pageSize - 1) / pageSize;
				pageIndex = pageIndex > 0 ? pageIndex : totalPages;
				return q.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
			}
		}

		/// <summary>
		/// 获取与一个关键字相关的其他关键字。
		/// </summary>
		/// <param name="keyword"></param>
		/// <returns></returns>
		public static KeywordInfo[] GetRelativeKeywords(this KeywordInfo keyword)
		{
			using (RxDataContext db = new RxDataContext())
			{
				var q = from rk1 in db.RegexKeywords
						join rk2 in db.RegexKeywords on rk1.RegexId equals rk2.RegexId
						join ki in db.KeywordInfos on rk2.KeywordId equals ki.KeywordId
						where rk1.KeywordId == keyword.KeywordId && rk2.KeywordId != keyword.KeywordId
						select ki;

				return q.Distinct().OrderBy(k => k.Text).ToArray();
			}
		}

		/// <summary>
		/// 获取用户使用过的关键字。
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public static KeywordInfo[] GetUsedKeywords(this UserInfo user)
		{
			using (RxDataContext db = new RxDataContext())
			{
				var q = from k in db.KeywordInfos
						join rk in db.RegexKeywords on k.KeywordId equals rk.KeywordId
						where rk.UserId == user.UserId
						select k;
				return q.Distinct().OrderBy(k => k.Text).ToArray();
			}
		}

		/// <summary>
		/// 获取最热门关键字。
		/// </summary>
		/// <param name="number">获取的关键字的数量。-1表示获取所有。</param>
		/// <returns></returns>
		public static KeywordRankWithLevel[] GetTopUseKeywords(int number)
		{
			using (RxDataContext db = new RxDataContext())
			{
				var q = from kr in db.KeywordRanks
						orderby kr.RegexCount descending
						select kr;

				var qkr = number > 0 ?
					q.Take(number).OrderBy(kr => kr.Text).ToArray() :
					q.OrderBy(kr => kr.Text).ToArray();

				var allRanks = qkr.Select(kr => (int)kr.RegexCount).ToArray();

				var result =  qkr.Select(kr => new KeywordRankWithLevel
				{
					KeywordRank = kr,
					RankLevel = RxRank.GetLevel(allRanks, (int)kr.RegexCount)
				})
				.OrderBy(krl=>krl.KeywordRank.Text)
				.ToArray();

				return result;
			}
		}

		/// <summary>
		/// 表示关键字的使用频度和频度等级。
		/// 频度等级决定了关键字在页面上显示的大小（使用的样式）。
		/// </summary>
		public class KeywordRankWithLevel
		{
			/// <summary>
			/// 关键字和使用频度。
			/// </summary>
			public KeywordRank KeywordRank { get; set; }

			/// <summary>
			/// 频度等级。
			/// </summary>
			public int RankLevel { get; set; }
		}
	}
}
